
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.tsx'
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper'

import { BrowserRouter } from 'react-router-dom'
let root: any = null;

function render(props: any = {}) {
  const { container } = props;
  const dom = container ? container.querySelector('#root') : document.getElementById('root');

  root = createRoot(dom!);
  root.render(
    <BrowserRouter basename={(window as any).__POWERED_BY_QIANKUN__ ? '/project' : '/'}>
      <App />
    </BrowserRouter>
  );
}

renderWithQiankun({
  mount(props) {
    console.log('[child-react-app1] mount', props);
    render(props);
  },
  bootstrap() {
    console.log('[child-react-app1] bootstrap');
  },
  unmount(props) {
    console.log('[child-react-app1] unmount', props);
    root.unmount();
  },
  update(props) {
    console.log('[child-react-app1] update', props);
    render(props)
  },
});

// 独立运行时
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render({});
}
